package cn.dakaqi.services.impl;

import cn.dakaqi.dao.PinganOpenDao;
import cn.dakaqi.entities.PinganOpen;
import cn.dakaqi.services.PinganOpenService;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * @author chunyang on 2016/4/19.
 */
@Component(value = "pinganOpenService")
@Transactional
public class PinganOpenServiceImpl implements PinganOpenService
{
    @Autowired
    PinganOpenDao pinganOpenDao;
    @Override
    public PinganOpen save(PinganOpen pinganOpenRecord) throws ServiceRuntimeException
    {
        return pinganOpenDao.save(pinganOpenRecord);
    }

    @Override
    public PinganOpen findByMobileAndIdCard(String mobile, String idCard) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("mobile", new SearchFilter("mobile", SearchFilter.Operator.EQ, mobile));
        filters.put("idCard", new SearchFilter("idCard", SearchFilter.Operator.EQ, idCard));
        Specification<PinganOpen> spec = DynamicSpecifications.bySearchFilter(filters.values(), PinganOpen.class);
        List<PinganOpen> list = this.pinganOpenDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public PinganOpen findByIdCard(String idCard) {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("idCard", new SearchFilter("idCard", SearchFilter.Operator.EQ, idCard));
        Specification<PinganOpen> spec = DynamicSpecifications.bySearchFilter(filters.values(), PinganOpen.class);
        List<PinganOpen> list = this.pinganOpenDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public PinganOpen findByGuardianIDCard(String guardianIDCard)
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("guardianIDCard", new SearchFilter("guardianIDCard", SearchFilter.Operator.EQ, guardianIDCard));
        Specification<PinganOpen> spec = DynamicSpecifications.bySearchFilter(filters.values(), PinganOpen.class);
        List<PinganOpen> list = this.pinganOpenDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public PinganOpen findByCardNum(String cardNum) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("cardNum", new SearchFilter("cardNum", SearchFilter.Operator.EQ, cardNum));
        Specification<PinganOpen> spec = DynamicSpecifications.bySearchFilter(filters.values(), PinganOpen.class);
        List<PinganOpen> list = this.pinganOpenDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public PinganOpen findByVolunteerCode(String volunteerCode)
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteerCode", new SearchFilter("volunteerCode", SearchFilter.Operator.EQ, volunteerCode));
        Specification<PinganOpen> spec = DynamicSpecifications.bySearchFilter(filters.values(), PinganOpen.class);
        List<PinganOpen> list = this.pinganOpenDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public PinganOpen findByCardNumAndStatus(String cardNum, int statusNo)
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("cardNum", new SearchFilter("cardNum", SearchFilter.Operator.EQ, cardNum));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, statusNo));
        Specification<PinganOpen> spec = DynamicSpecifications.bySearchFilter(filters.values(), PinganOpen.class);
        List<PinganOpen> list = this.pinganOpenDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public PinganOpen findByVolunteerCodeAndStatus(String volunteerCode, int statusYes)
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteerCode", new SearchFilter("volunteerCode", SearchFilter.Operator.EQ, volunteerCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, statusYes));
        Specification<PinganOpen> spec = DynamicSpecifications.bySearchFilter(filters.values(), PinganOpen.class);
        List<PinganOpen> list = this.pinganOpenDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }
}
